package com.Xht.www.servlet;

//后端响应修改商家信息的请求

import com.Xht.www.dao.MerchantInfoDAO;
import com.Xht.www.po.MerchantInfoPO;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.Year;
import java.util.Enumeration;

@WebServlet("/EditMerchantInfoServlet")
@MultipartConfig
public class EditMerchantInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 打印请求的所有参数名，用于调试
        Enumeration<String> parameterNames = request.getParameterNames();
        System.out.println("请求参数列表:");
        while (parameterNames.hasMoreElements()) {
            String paramName = parameterNames.nextElement();
            System.out.println("参数名: " + paramName + ", 值: " + request.getParameter(paramName));
        }

        // 获取商家名称
        String merchantName = request.getParameter("merchantName");
        System.out.println(
                "商家名称: " + merchantName +
                        ", 新名称: " + request.getParameter("newName") +
                        ", 新密码: " + request.getParameter("newPassword") +
                        ", 新地址: " + request.getParameter("newAddress") +
                        ", 新establishmentYear: " + request.getParameter("newEstablishmentYear") +
                        ", 新联系方式: " + request.getParameter("newContactInfo") +
                        ", 新关键词: " + request.getParameter("newKeywords")
        );

        // 非空检查
        if (merchantName == null || merchantName.isEmpty()) {
            System.out.println("未提供有效的商家名称");
            response.getWriter().write("未提供有效的商家名称");
            return;
        }

        MerchantInfoDAO merchantInfoDAO = new MerchantInfoDAO();
        // 根据商家名称查询商家 ID
        Integer merchantId = merchantInfoDAO.getMerchantIdByName(merchantName);

        if (merchantId == null) {
            System.out.println("未找到该商家信息");
            response.getWriter().write("未找到该商家信息");
            return;
        }

        // 根据商家 ID 查找商家信息
        MerchantInfoPO merchant = merchantInfoDAO.getMerchantById(merchantId);

        if (merchant == null) {
            System.out.println("未找到该商家信息");
            response.getWriter().write("未找到该商家信息");
            return;
        }

        // 获取新的信息
        String newName = request.getParameter("newName");
        String newPassword = request.getParameter("newPassword");
        String newAddress = request.getParameter("newAddress");
        String newEstablishmentYearStr = request.getParameter("newEstablishmentYear");
        String newContactInfo = request.getParameter("newContactInfo");
        String newKeywords = request.getParameter("newKeywords");

        // 非空检查
        if ((newName != null && newName.isEmpty()) ||
                (newPassword != null && newPassword.isEmpty()) ||
                (newAddress != null && newAddress.isEmpty()) ||
                (newContactInfo != null && newContactInfo.isEmpty()) ||
                (newKeywords != null && newKeywords.isEmpty())) {
            System.out.println("新信息不能为空");
            response.getWriter().write("新信息不能为空");
            return;
        }

        // 更新商家信息
        if (newName != null) {
            merchant.setMerchantName(newName);
        }
        if (newPassword != null) {
            merchant.setPassword(newPassword);
        }
        if (newAddress != null) {
            merchant.setAddress(newAddress);
        }
        if (newEstablishmentYearStr != null && !newEstablishmentYearStr.isEmpty()) {
            try {
                int newEstablishmentYear = Integer.parseInt(newEstablishmentYearStr);
                int currentYear = Year.now().getValue();
                int earliestYear = currentYear - 130;
                if (newEstablishmentYear > earliestYear && newEstablishmentYear <= currentYear) {
                    merchant.setEstablishmentYear(newEstablishmentYear);
                } else {
                    System.out.println("建立年份必须在合理范围内（不晚于当前时间且不早于130年前）");
                    response.getWriter().write("建立年份必须在合理范围内（不晚于当前时间且不早于130年前）");
                    return;
                }
            } catch (NumberFormatException e) {
                System.out.println("建立年份必须是有效的整数");
                response.getWriter().write("建立年份必须是有效的整数");
                return;
            }
        }
        if (newContactInfo != null) {
            merchant.setContactInfo(newContactInfo);
        }
        if (newKeywords != null) {
            merchant.setKeywords(newKeywords);
        }

        // 更新商家信息到数据库
        merchantInfoDAO.updateMerchantInfo(merchant);
        System.out.println("店铺信息修改成功");
        response.getWriter().write("店铺信息修改成功");
    }
}